package com.uzhie.web.controller.websocket;

import com.alibaba.fastjson.JSONObject;
import com.uzhie.web.module.User;
import com.uzhie.util.SessionManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by TangFei on 2018/8/4.
 */
public class SocketHandler extends AbstractWebSocketHandler {

    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private SessionManager<User> sessionManager;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        logger.info("enter method afterConnectionEstablished " + session.getId());
        super.afterConnectionEstablished(session);
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        try {
            logger.info("enter method handleMessage");
            Object httpSessionID = session.getAttributes().get("webSessionID");
            logger.info("socketHandler webSessionID is " + httpSessionID);
            SessionManager<User>.Session<User> userSession = sessionManager.get((String) session.getAttributes().get("userId"));
            Map<String, WebSocketSession> socketSessionMap = userSession.getSocketSessionMap();
            if (null == socketSessionMap) {
                socketSessionMap = new HashMap<>();
            }
            socketSessionMap.put((String) httpSessionID, session);
            userSession.setSocketSessionMap(socketSessionMap);
            sessionManager.setSocketSession((String) session.getAttributes().get("userId"), userSession);
            super.handleMessage(session, message);
        } catch (Throwable var4) {
            logger.error(var4);
            session.sendMessage(new TextMessage("error"));
        }
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        logger.info("enter method handleTextMessage");
        String payload = message.getPayload();
        JSONObject object = JSONObject.parseObject(payload);
        logger.info("payload is " + object);
        super.handleTextMessage(session, message);
    }

    @Override
    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
        logger.info("enter method handleBinaryMessage");
        super.handleBinaryMessage(session, message);
    }

    @Override
    protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
        logger.info("enter method handlePongMessage");
        super.handlePongMessage(session, message);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        logger.info("enter method handleTransportError");
        super.handleTransportError(session, exception);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        logger.info("enter method afterConnectionClosed");
        super.afterConnectionClosed(session, status);
    }
}
